<?php

namespace App\Http\Model;

use http\Env\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UnionSigning extends Base
{

    protected $table = "union_signing";
    protected $table_log = "union_signing_log";
    protected $redis_cache_gift_union_room_stream = "CACHE:GIFT:UNION:ROOM:STREAM:"; //公会统计礼物缓存
    protected $redis_cache_gift_union_user_stream = "CACHE:GIFT:UNION:USER:STREAM:"; //公会统计礼物缓存

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //获取签约 月份
    public function getSignMonth()
    {
        $arr = [6, 12];
        return $arr;
    }

    /*
     * 生成签约记录
     * @param $user_id    用户id
     * @param $union_id   公会id
     * @param $month      申请月份
     * @param $type       0 申请  1续约  2解约
     * */
    public function createLog($user_id, $union_id, $month, $type = 0)
    {
        $data['user_id'] = $user_id;
        $data['un_id'] = $union_id;
        $data['apply_month'] = $month;
        $data['type'] = $type;
        return DB::table($this->table_log)->insert($data);
    }


    /*
     * 获取申请记录
     * @param $user_id    用户id
     * @param $type       0 签约  1续约 2解约
     * @param $union_id   公会id
     * */
    public function getApplyLog($user_id = '', $type = 0, $union_id = '')
    {
        $where['type'] = $type;
        if ($user_id != '') $where['user_id'] = $user_id;
        if ($union_id != '') $where['un_id'] = $union_id;
        $res = DB::table($this->table_log)->where($where)->orderBy('created_at', 'desc')->get();
        return $this->objToArray($res);
    }

    /*
     * 删除公会签约记录
     * @param $union_id   公会id
     * @param $user_id    用户id
     * */
    public function delSignLog($union_id = 0, $user_id = 0)
    {
        $where = [];
        if ($union_id != 0) $where['un_id'] = $union_id;
        if ($user_id != 0) $where['user_id'] = $user_id;
        if (empty($where)) return true;
        return DB::table($this->table_log)->where($where)->delete();
    }


    /*
     * 签约公会
     * @param $user_id    用户id
     * @param $union_id   公会id
     * @param $month      申请月份
     * @param $type       0 申请  1续约
     * */
    public function createSign($user_id, $union_id, $month, $type = 0)
    {
        $data['user_id'] = $user_id;
        $data['un_id'] = $union_id;
        $data['month'] = $month;
        $data['expire_time'] = strtotime("+" . $month . " month", time());
        $data['type'] = $type;
        $data['updated_at'] = $data['created_at'] = time();
        return DB::table($this->table)->insert($data);
    }


    /*
    * 续约公会
    * @param $expire_time    签约到期时间
    * @param $month          续约月份
    * */
    public function renewSign($id, $expire_time, $month, $old_month)
    {
        $data['expire_time'] = strtotime("+" . $month . " month", $expire_time);
        $data['month'] = $month + $old_month;
        $data['type'] = 1;
        $data['updated_at'] = time();
        return DB::table($this->table)->where(['id' => $id])->update($data);
    }

    /*
     * 获取用户签约信息  多条
     * @param $user_ids  arr  用户id
     * */
    public function getSignToUserIds($user_ids = [])
    {
        $where[] = ['expire_time', '>=', time()];
        $res = DB::table($this->table)->where($where)->whereIn('user_id', $user_ids)->get();
        return $this->objToArray($res);
    }

    /*
      * 获取单个公会用户签约信息
      * @param $user_ids  arr  用户id
      * */
    public function getUnionSignToUserId($user_id, $union_id = 0, $expire_time = true)
    {
        $where['user_id'] = $user_id;
        if ($expire_time == true) $where[] = ['expire_time', '>=', time()];
        if ($union_id != 0) $where['un_id'] = $union_id;
        $res = DB::table($this->table)->where($where)->first();
        return $this->objToArray($res);
    }


    /*
     * 获取公会签约用户列表
     * @param $union_id   公会id
     * */
    public function getUnionSign($union_id, $user_id = 0, $page = 0, $page_size = 10)
    {
        $where[] = ['expire_time', '>=', time()];
        if ($user_id != 0) {
            $where['user_id'] = $user_id;
        }
        $where['un_id'] = $union_id;
        $res = DB::table($this->table)->where($where)
            ->offset(($page) * $page_size)
            ->limit($page_size)
            ->get();
        return $this->objToArray($res);
    }

    /*
     * 删除公会签约用户
     *
     * */
    public function delSign($union_id = 0, $user_id = 0)
    {
        $where = [];
        if ($union_id != 0) $where['un_id'] = $union_id;
        if ($user_id != 0) $where['user_id'] = $user_id;
        if (empty($where)) return true;
        return DB::table($this->table)->where($where)->delete();
    }


    public function signingUserUpMonthTotal($user_id, $value = 1)
    {
        $month = date("Y-m", strtotime("-" . $value . " month", time()));
        $key = $this->redis_cache_gift_union_user_stream . $month;
        $total = Redis::HGET($key, $user_id) ?? 0;
        return $total / 100;
    }

    public function signingRoomUpMonthTotal($room_ids = [], $value = 1)
    {
        $month = date("Y-m", strtotime("-" . $value . " month", time()));
        $key = $this->redis_cache_gift_union_room_stream . $month;
        $total = 0;
        foreach ($room_ids as $v) {
            $total += Redis::HGET($key, $v);
        }
        return $total / 100;
    }

    /**删除签约过期的记录
     * @param $user_id
     */
    public function delSignExpire($user_id){
        $where[] = ['user_id' ,'=' ,$user_id];
        $where[] = ['expire_time', '<', time()];
        DB::table($this->table)->where($where)->delete();
    }

    /**获取公会签约人数
     * @param $union_id
     * @return int|mixed
     */
    public function getUnionSignNum($union_id)
    {
        $where[] = ['expire_time', '>=', time()];
        $where['un_id'] = $union_id;
        $res = DB::table($this->table)
            ->select(DB::raw('count(user_id) as num'))
            ->where($where)
            ->get();
        $res = $this->objToArray($res);
        return empty($res) ? 0 : $res[0]['num'];
    }

    /*
     * 批量删除公会签约用户
     *
     * */
    public function batchDelSign( array $user_id = [])
    {
        return DB::table($this->table)->whereIn('user_id', $user_id)->delete();
    }



    public function sendGoodsLog(int $union_id, int $user_id, int $goods_id, int $type=0, string $desc='')
    {
        $data['user_id'] = $user_id;
        $data['union_id'] = $union_id;
        $data['goods_id'] = $goods_id;
        $data['desc'] = $desc;
        $data['add_time'] = time();
        return DB::table($this->table_send_goods_log)->insert($data);
    }

}
